package com.miaowang.salad.util;

import java.util.UUID;

import prj.carmap.mobile.CarmapApplication;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.miaowang.salad.Common;

public class ActivityUtils {

	/**
	 * 退出应用
	 */
	public static void exitApp(Activity activity) {
		CarmapApplication app = (CarmapApplication) activity.getApplication();
		// app.onTerminate();
		// System.exit(0);
		activity.finish();
	}

	public static String generateUuid() {
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		Log.i(Common.TAG, uuid + " randomUuid");
		if (CheckUtil.isEmpty(uuid)) {
			uuid = String.valueOf(System.currentTimeMillis());
		}
		return uuid;
	}

	/**
	 * 获得设备ID
	 * 
	 * @param context
	 * @return
	 */
	public static String getDeviceId(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String id = telephonyManager.getDeviceId();
		String uuid = getUUID(context);
		if (CheckUtil.isEmpty(id)) {
			id = uuid;
		}
		return uuid + "|" + id;
		// return id;
	}

	/**
	 * 获得UUID
	 * 
	 * @param context
	 * @return
	 */
	public static String getUUID(Context context) {
		String uuid = "";
		// SD卡存在UUID时，检查缓存中是否存有UUID
		String uuidInPref = PrefUtils.getString(context, Common.UUID, "");
		if (CheckUtil.isEmpty(uuidInPref)) {
			uuid = UUID.randomUUID().toString();
			PrefUtils.putString(context, Common.UUID, uuid);
		}
		return uuid;
	}
}
